package org.stwy.lease.rent;

import java.util.Date;
import java.util.List;

import javax.servlet.ServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.stwy.lease.contract.ContractService;

import com.rap.common.exception.ServiceException;
import com.rap.common.utils.DateUtil;
import com.rap.frame.web.ActionResultModel;
import com.rap.frame.web.BaseController;

/*
 * 租金收缴 控制类
 */
/**
*版权信息 (c) RAP 保留所有权利.
*@author Rap.Codegen
*@Team RAP Team
*@Date 2015-4-25 12:31:24
*<p></p>
*
*@参与人：Rap.Codegen
*/

@Controller
@RequestMapping(value = "/lease/rent")
public class RentController extends BaseController<RentEntity> {

	@Autowired
	private ContractService contractService;
	
	public RentController() {
		setResourceIdentity("stwy:lease:rent");
	}
	private RentService getService(){
		return (RentService)super.baseService;
	}

	@RequestMapping(method = RequestMethod.GET)
	public String view(Model model) {
		super.assertHasViewPermission();
		return "stwy/lease/rent/rent_main";
	}

	@RequestMapping(value = "/select")
	public String select(Model model, String callback) {
		model.addAttribute("callback", callback);
		return "stwy/lease/rent/rent_select";
	}
	
	@SuppressWarnings({ "rawtypes" })
	@RequestMapping(value = "/findToPayRecords")
	@ResponseBody
	public ActionResultModel findToPayRecords(ServletRequest request, Model model) {
		ActionResultModel arm = new ActionResultModel();
		try {
			List<RentEntity> list = getService().findToPayRecords(DateUtil.getCurDate());
			arm.setRecords(list);
			arm.setSuccess(true);
		} catch (ServiceException e) {
			e.printStackTrace();
			arm.setSuccess(false);
			arm.setMsg(e.getMessage());
		}catch (Exception e) {
			arm.setSuccess(false);
			arm.setMsg(e.getMessage());
			e.printStackTrace();
		}

		return arm;
	}
	@SuppressWarnings({ "rawtypes" })
	@RequestMapping(value = "/saveRents")
	@ResponseBody
	public ActionResultModel saveRents(ServletRequest request, Model model
			,@RequestParam(value = "uuid[]",required=true)String[] uuid
			,@RequestParam(value = "endDate[]",required=true)String[] endDate
			,@RequestParam(value = "repayMoney[]",required=true)String[] repayMoney
			,@RequestParam(value = "prepayMoney[]",required=true)String[] prepayMoney
			,@RequestParam(value = "payDate[]",required=true)String[] payDate
			,@RequestParam(value = "money[]",required=true)String[] money
			,@RequestParam(value = "leave[]",required=true)String[] leave
			,@RequestParam(value = "depositNow[]",required=true)String[] depositNow
			,@RequestParam(value = "remark[]",required=false)String[] remarks) {
		ActionResultModel arm = new ActionResultModel();
		try {
			getService().saveRents(uuid,payDate,money,leave,depositNow,endDate,repayMoney,prepayMoney,remarks);
			arm.setSuccess(true);
		} catch (ServiceException e) {
			e.printStackTrace();
			arm.setSuccess(false);
			arm.setMsg(e.getMessage());
		}catch (Exception e) {
			arm.setSuccess(false);
			arm.setMsg(e.getMessage());
			e.printStackTrace();
		}

		return arm;
	}
}
